<%@ include file="/WEB-INF/jsp/includes.jsp" %>
<%@ include file="/WEB-INF/jsp/header.jsp" %>
<script  src="/admin/js/dhtmlxtree_std.js"></script>
<script type="text/javascript">
var usrform;
function showform(id, uid) {
  var nf = id.length == '';
  if (!nf) {
    document.getElementById('user' + id).appendChild(usrform);
    var tr = document.getElementById('tr' + id);
    var recs = tr.getElementsByTagName('td');
    var u = document.uform;
    u.USERNAME.value = recs[0].childNodes[0].nodeValue;
    var rn = recs[1].childNodes[0];
    u.REALNAME.value = rn == null ? '' : rn.nodeValue;
    var dp = recs[2].childNodes[0];
    u.DEPARTMENT.value = dp == null ? '' : dp.nodeValue;
    var email = recs[3].childNodes[0];
    u.EMAIL.value = email == null ? '' : email.nodeValue;
    var tl = recs[4].childNodes[0];
    u.TITLE.value = tl == null ? '' : tl.nodeValue;
  } else {
    document.getElementById('newform').appendChild(usrform);
    var u = document.uform;
    u.USERNAME.value = '';
    u.REALNAME.value = '';
    u.DEPARTMENT.value = '';
    u.EMAIL.value = '';
    u.TITLE.value = '';
  }
  u.PASSWORD.value = '-----';
  u.ID.value = uid;
}
function usrupdate() {
  var u = document.uform;
  if (u.PASSWORD.value == '-----') {
    if (u.ID.value == -1) {
      alert('<fmt:message key="usermgmt.enterpasswd"/>');
      return;
    } else
      u.PASSWORD.value = '';
  }
  dhtmlxAjax.get('/admin/updateUser.do?ID=' + escape(u.ID.value)
    + '&USERNAME=' + escape(u.USERNAME.value) + '&REALNAME='
    + escape(u.REALNAME.value) + '&DEPARTMENT=' + escape(u.DEPARTMENT.value)
    + '&EMAIL=' + escape(u.EMAIL.value) + '&TITLE=' + escape(u.TITLE.value)
    + '&PASSWORD=' + escape(u.PASSWORD.value), function(ld) {
    var resp = ld.xmlDoc.responseText;
    alert(resp);
    if (resp.indexOf("SUCCESS") == 0)
      window.location = '/admin/usermgmt.do';
  });
}
function deluser(uid, name) {
  if (!confirm(name + ': <fmt:message key="usermgmt.deluser"/>'))
    return;
  dhtmlxAjax.get('/admin/deleteUser.do?ID=' + uid, function(ld) {
    var resp = ld.xmlDoc.responseText;
    alert(resp);
    if (resp.indexOf("SUCCESS") == 0)
      window.location = '/admin/usermgmt.do';
  });
}
function detachform() {
  usrform = document.getElementById('userform'); 
  usrform.parentNode.removeChild(usrform);
}
</script>
</head>
<body id="body" onLoad="detachform();">
<table border="0">
  <tr>
    <td align="left"><input type="button" onClick="showform('', -1);"
     value="<fmt:message key="usermgmt.add"/>"></input></td>
  </tr>
</table>
<div id="newform"></div>
<br/>
<br/>
<center>
<c:forEach var="user" items="${userModel.adminUsers}" varStatus="ls">
  <table id="bundles">
    <c:if test="${ls.index == 0}">
      <tr>
        <th><fmt:message key="usermgmt.username"/></th>
        <th><fmt:message key="usermgmt.realname"/></th>
        <th><fmt:message key="usermgmt.department"/></th>
        <th><fmt:message key="usermgmt.email"/></th>
        <th><fmt:message key="usermgmt.title"/></th>
        <th><fmt:message key="usermgmt.lastlogin"/></th>
        <th><fmt:message key="usermgmt.operations"/></th>
      </tr>
    </c:if>
    <tr class="${ls.index % 2 == 0 ? 'even' : 'odd'}" id="tr${ls.index}">
      <td width="8%">${user.username}</td>
      <td width="15%">${user.realname}</td>
      <td width="18%">${user.department}</td>
      <td width="18%">${user.email}</td>
      <td width="16%">${user.title}</td>
      <td width="12%">${user.timeLastLogin}</td>
      <td width="5%">
       <table id="userops">
         <tr>
           <td><input type="button" onClick="showform('${ls.index}', ${user.id});"
            value="<fmt:message key="usermgmt.modify"/>"></input></td>
           <td width="4%"><input type="button" onClick="deluser('${user.id}', '${user.username}')"
            value="<fmt:message key="usermgmt.delete"/>"></input></td>
         </tr>
       </table>
    </tr>
  </table>
</center>
<div id="user${ls.index}"></div>
</c:forEach>
<div id="userform">
  <form name="uform" method="post" action="/admin/updateUser.do">
    <input type="hidden" name="ID"/>
    <table>
      <tr>
        <td><label><fmt:message key="usermgmt.username"/>:</label></td>
        <td><input type="text" name="USERNAME" size="25"/></td>
      </tr>
      <tr>
        <td><label><fmt:message key="usermgmt.realname"/>:</label></td>
        <td><input type="text" name="REALNAME" size="25"/></td>
      </tr>
      <tr>
        <td><label><fmt:message key="usermgmt.department"/>:</label></td>
        <td><input type="text" name="DEPARTMENT" size="25"/></td>
      </tr>
      <tr>
        <td><label><fmt:message key="usermgmt.email"/>:</label></td>
        <td><input type="text" name="EMAIL" size="25"/></td>
      </tr>
      <tr>
        <td><label><fmt:message key="usermgmt.title"/>:</label></td>
        <td><input type="text" name="TITLE" size="25"/></td>
      </tr>
      <tr>
        <td><label><fmt:message key="usermgmt.password"/>:</label></td>
        <td><input type="password" name="PASSWORD" size="25"/></td>
      </tr>
      <tr>
        <td/>
        <td>
          <table width="100%">
            <tr>
              <td>
                <input type="button" value="<fmt:message key="usermgmt.update"/>"
                 onClick="usrupdate();" style="width:100%;"/>
              </td>
              <td>
                <input type="button" value="<fmt:message key="usermgmt.cancel"/>"
                 onClick="detachform();" style="width:100%;"/> 
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </form>
</div>
</body>
</html>
